package com.bdzt.beidoubox.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.bdzt.beidoubox.R;
import com.bdzt.beidoubox.activity.FamilyListActivity;
import com.bdzt.beidoubox.activity.IdeaActivity;
import com.bdzt.beidoubox.activity.TeamBoxListActivity;
import com.bdzt.beidoubox.utils.NetUtils;
import com.bdzt.beidoubox.utils.T;


/**
 * Created by 、默 on 3/28/2017.
 */

public class SetFragment extends Fragment implements View.OnClickListener {
    private Context context;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context = getActivity();
        View view = inflater.inflate(R.layout.fragment_set, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        view.findViewById(R.id.rl_setBoxID).setOnClickListener(this);
        view.findViewById(R.id.rl_aboutUs).setOnClickListener(this);
        view.findViewById(R.id.rl_idea).setOnClickListener(this);
        view.findViewById(R.id.rl_setBoxName).setOnClickListener(this);
        view.findViewById(R.id.rl_setFolkInfo).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (!NetUtils.isConnect(context)) {
            T.showShort(context, "本功能需要连接公网,请检查您的网络!");
            return;
        }
        ;
        switch (view.getId()) {
            case R.id.rl_aboutUs:
                break;
            case R.id.rl_idea:
                startActivity(new Intent(context, IdeaActivity.class));
                break;
            case R.id.rl_setBoxID:
                startActivity(new Intent(context, TeamBoxListActivity.class));
                break;
            case R.id.rl_setBoxName:
                final EditText et = new EditText(context);
                final AlertDialog alertDialog = createDialog("请输入新的盒子名子", et);
                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String temp = et.getText().toString();
                        if (TextUtils.isEmpty(temp)) {
                            T.showShort(context, "请输入新名字！");
                        } else {
                            alertDialog.dismiss();
                        }
                    }
                });
                break;
            case R.id.rl_setFolkInfo:
                startActivity(new Intent(context, FamilyListActivity.class));
                break;
        }
    }

    public AlertDialog createDialog(String title, EditText et) {
        return new AlertDialog.Builder(context).setTitle(title).setView(et)
                .setPositiveButton(R.string.ok, null)
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();

    }
}
